package com.zysc.processflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zysc.processflow.common.util.StringUtils;
import com.zysc.processflow.entity.*;
import com.zysc.processflow.mapper.PersonroleMapper;
import com.zysc.processflow.service.IPersonService;
import com.zysc.processflow.service.IPersonroleService;
import com.zysc.processflow.common.base.BaseService;
import com.zysc.processflow.service.IRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 人员角色表 服务实现类
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-23
 */
@Service
public class PersonroleServiceImpl extends BaseService<PersonroleMapper, Personrole> implements IPersonroleService {

    @Autowired
    IRoleService roleService;

    @Autowired
    IPersonService personService;
    @Override
    public Boolean DeletePersonroleByPersonidAndRoleid(Personrole entity) {
        boolean b = true;
        String personid = entity.getPersonid();
        if(StringUtils.isNull(personid)){
            throw new RuntimeException("缺少人员唯一标识符");
        }
        String roleid = entity.getRoleid();
        String orgid = entity.getOrgid();
        List<Personrole> list = new ArrayList<>();
        LambdaQueryWrapper<Personrole> qw = new LambdaQueryWrapper<>();
        qw.eq(Personrole::getPersonid,personid)
                .eq(!StringUtils.isNull(roleid),Personrole::getRoleid,roleid)
                .eq(!StringUtils.isNull(orgid),Personrole::getOrgid,orgid)
                .ne(Personrole::getOrgid,"admin");
        list = this.list(qw);
        if(list.size()>0){
            b = this.remove(qw);
        }
        return b;
    }

    @Override
    public Boolean saveOrUpdatePersonroleInfo(Personrole entity) {
        boolean b = true;
        String personid = entity.getPersonid();
        String roleid = entity.getRoleid();
        String orgid = entity.getOrgid();
        if(StringUtils.isNull(personid) || StringUtils.isNull(roleid)){
            throw new RuntimeException("缺少人员或者职位数据");
        }
        List<Personrole> list = new ArrayList<>();
        LambdaQueryWrapper<Personrole> qw = new LambdaQueryWrapper<>();
        qw.eq(!StringUtils.isNull(personid),Personrole::getPersonid,personid)
                .eq(!StringUtils.isNull(roleid),Personrole::getRoleid,roleid)
                .eq(!StringUtils.isNull(orgid),Personrole::getOrgid,orgid);
        list = this.list(qw);
        if(list.size()>0){
            entity.setId(list.get(0).getId());
        }
        b = this.saveOrUpdate(entity);
        return b;
    }

    @Override
    public Boolean SaveOrUpdatePersonRole(String personid, String personname, String orgid, String orgname, List<String> roleidlist) {
        Boolean b1 = true;//1.删除该人员该单位的所有角色信息
        Boolean b2 = true;//2.添加信息

        //1.删除该人员在该单位的所有角色信息
        List<Personrole> list = new ArrayList<>();
        LambdaQueryWrapper<Personrole> qw = new LambdaQueryWrapper<>();
        qw.eq(!StringUtils.isNull(personid),Personrole::getPersonid,personid);
        // .eq(!StringUtils.isNull(orgid),Personrole::getOrgid,orgid);
        list = this.list(qw);
        if(list.size()>0){
            b1 = this.remove(qw);
        }

        List<Personrole> personroleList = new ArrayList<>();
        //2.添加信息
        Personrole personroleTemp = new Personrole();
        personroleTemp.setOrgid(orgid);
        personroleTemp.setOrgname(orgname);
        personroleTemp.setPersonid(personid);
        personroleTemp.setPersonname(personname);

        for(int i = 0;i< roleidlist.size();i++){
            String roleid = roleidlist.get(i);
            String rolename = "";

            LambdaQueryWrapper<Role> roleqw = new LambdaQueryWrapper<>();
            roleqw.eq(Role::getRolekey,roleid);
            List<Role> roleList = roleService.list(roleqw);
            if(roleList.size()>0){
                rolename = roleList.get(0).getRolename();
            }

            Personrole personrole = new Personrole();
            BeanUtils.copyProperties(personroleTemp,personrole);
            personrole.setRoleid(roleid);
            personrole.setRolename(rolename);
            personroleList.add(personrole);
        }
        if(personroleList.size()>0){
            b2 = this.saveBatch(personroleList);
        }
        return b1&&b2;
    }

    @Override
    public List<Role> getroleListByPersonid(String personid) {
        //判断事件中心里有没有这个人,如果没有那么返回空数组
        LambdaQueryWrapper<Person> personqw = new LambdaQueryWrapper<>();
        personqw.eq(Person::getId,personid);
        List<Person> personList = personService.list(personqw);
        if(personList.size()==0){
            return new ArrayList<>();
        }
        List<Role> roleList = this.baseMapper.getroleListByPersonid(personid);
        if(roleList.size()==0){
            LambdaQueryWrapper<Role> roleqw = new LambdaQueryWrapper<>();
            roleqw.eq(Role::getRolekey,"processflow_visitor");
            roleList = roleService.list(roleqw);
        }
        return roleList;
    }
}
